<template>
  <ScrollPage>
    <view class="page-integral">
      <view class="top-bg">
        <image class="bg" :src="`${ASSETS_URL}hugging-face.png`" mode="aspectFit" />
        <image class="emojis" :src="`${ASSETS_URL}hugging-face.png`" mode="aspectFit" />
        <view class="text">
          你好，{{ selectedChild.name }}让我猜猜你会兑换什么
        </view>
      </view>
      <view class="integral-container">
        <template v-if="list.length">
          <view v-for="item in list" :key="item.id" class="item" @click="selectItem(item)">
            <view class="post">
              <image :src="`${ASSETS_URL}hugging-face.png`" mode="aspectFit" />
            </view>
            <view class="text">
              {{ item.name }}
            </view>
            <view class="operation">
              <view class="col-l">
                {{ item.point }}
              </view>
              <button class="col-r" @click.stop="exchange(item)">
                兑换
              </button>
            </view>
          </view>
        </template>
        <wd-status-tip v-else image="message" tip="没有任何内容哦" />
      </view>

      <view class="empty-container" />
    </view>
    <template #bottom>
      <button class="add-btn" @click="show = true">
        添加
      </button>
    </template>
  </ScrollPage>
  <SheetCom v-model="show" :show-remove="!!form.id" :title="`${form.id ? '编辑' : '添加'}信息`" @remove="remove" @submit="submit" @close="reset">
    <FormItem title="名称">
      <wd-input v-model="form.name" :cursor-spacing="200" placeholder="请输入" />
    </FormItem>
    <FormItem title="积分">
      <wd-input v-model="form.point" type="number" :cursor-spacing="200" placeholder="请输入" />
    </FormItem>
  </SheetCom>
</template>

<script lang="ts" setup>
import type { IIntegralInfo } from '@/api/integral'
import { deepClone } from '@alova/shared'
import { storeToRefs } from 'pinia'
import { points, PointsAction, pointsList, pointsTransaction, removePoint } from '@/api/integral'
import FormItem from '@/components/FormItem.vue'
import SheetCom from '@/components/Sheet.vue'
import { ASSETS_URL } from '@/constants'
import ScrollPage from '@/layouts/ScrollPage.vue'
import { useChildStore } from '@/store/child'

defineOptions({
  name: 'Integral',
})

const list = ref<IIntegralInfo[]>([])

const defaultData: IIntegralInfo = {
  name: '',
  point: 1,
}

const show = ref(false)
const form = ref(defaultData)

definePage({
  style: {
    // 'custom' 表示开启自定义导航栏，默认 'default'
    navigationStyle: 'default',
    navigationBarTitleText: '积分中心',
    backgroundColor: '#fafafa',
  },
})
// #region Props
// #endregion
// #region Emits
// #endregion
// #region Hooks
const childStore = useChildStore()
const { selectedChild } = storeToRefs(childStore)
// #endregion
// #region Computed
// #endregion
// #region Watch
// #endregion
// #region Event
// #endregion
// #region Function
function exchange(item: IIntegralInfo) {
  uni.showModal({
    content: `确认用${item.point}兑换${item.name}吗?`,
    success: (res) => {
      if (res.confirm) {
        pointsTransaction({
          childId: selectedChild.value.id,
          id: item.id,
          type: PointsAction.Exchange,
        })
      }
    },
  })
}

function selectItem(item: IIntegralInfo) {
  form.value = deepClone(item)
  show.value = true
}

function refresh() {
  pointsList().then((res) => {
    list.value = res
  })
}

function reset() {
  form.value = deepClone(defaultData)
  show.value = false
}

function remove(cb: () => void) {
  removePoint({ id: form.value.id }).then(() => {
    refresh()
    reset()
    cb()
  })
}
function submit(cb: () => void) {
  if (!form.value.name || !form.value.point) {
    cb()
    return uni.showToast({
      title: '请填写完整信息',
      icon: 'error',
    })
  }

  if (!/^[1-9]\d*$/.test(form.value.point.toString())) {
    cb()
    return uni.showToast({
      title: '无效的分数',
      icon: 'error',
    })
  }

  points({
    ...form.value,
  }).then(() => {
    form.value = defaultData
    refresh()
    reset()
    cb()
  })
}
// #endregion
// #region Life Cycle
onLoad(() => {
  refresh()
})
// #endregion
// #region Expose
// #endregion
</script>

<style lang="scss" scoped>
.page-integral {
  display: flex;
  flex-direction: column;
  height: 100%;
  .top-bg {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 50px;
    flex: 0 0 auto;
    .bg {
      position: absolute;
      filter: blur(100px);
      z-index: 1;
      transform: scale(1);
    }
    .emojis {
      width: 126px;
      height: 118px;
      position: relative;
      z-index: 2;
      margin-bottom: 10px;
    }
    .text {
      position: relative;
      z-index: 2;
      font-weight: 600;
      font-size: 16px;
    }
  }
  .integral-container {
    min-height: 50%;
    background: var(--card-border-color);
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    position: relative;
    z-index: 2;
    display: flex;
    flex-wrap: wrap;
    padding: 20px;
    box-sizing: border-box;
    gap: 20px;
    flex: 1 0 auto;
    .item {
      width: calc((100% - 20px) / 2);
      .post {
        background: rgba($color: #000000, $alpha: 0.1);
        width: 100%;
        height: 160px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 8px;
        box-sizing: border-box;
        padding: 20px;
      }
      .text {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font: 16px;
        color: var(--text-color);
        margin: 20px 0 25px 0;
      }
      .operation {
        display: flex;
        align-items: center;
        justify-content: space-between;
        .col-l {
          font-size: 24px;
          color: var(--light-color);
        }
        .col-r {
          width: 70px;
          height: 30px;
          background: var(--button-color);
          font-size: 16px;
          color: var(--text-color);
          display: flex;
          align-items: center;
          justify-content: center;
          border-radius: 30px;
          padding: 0;
          margin: 0;
        }
      }
    }
  }
  .empty-container {
    height: 155px;
    flex: 0 0 auto;
  }
}
.add-btn {
  height: 60px;
  background: var(--button-color);
  margin: 20px 20px 0 20px;
  border-radius: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
